﻿Chenxin.UI.PermitManage=Chenxin.extend(Chenxin.UI.PageCommon,{
    autoInitForm:false,
    actionUrl:'/service/permitMange.ashx',
    createForm:function(){
        return new Chenxin.UI.EditPermitForm({
			            scope:this,
                        afterGetFormJson:function(json){
                            if(this.op=='Add'){
                                delete json.PermitID;
                                delete json.hidPermitCode;
                            }else{
                                delete json.hidPermitCode;
                            }
                        }
		            });
    },
    initEvent:function(){
        var _this=this;
        /*************************事件：权限添加************************/
        $("#aAddPermit").click(function(){

            //删除按钮非显示
            $("#btDelEditPermit").hide();
            
            if (_this.form.show() == false) return;
            
		    //显示权限编辑
		    _this.form.show();
		    _this.form.reset();
			_this.form.setTitle('权限添加');
        });
        
        /*************************事件：权限编辑************************/
        $("#divPermitContent").click(function(event){
        
            if(event.target.className == "clsEditPermit"){
                //待处理的参数
                var strDealParam = $(event.target).attr("href");
                //井号位置
                var idxPound;
                //取得#号位置
                idxPound = strDealParam.indexOf("#");
                //权限ID
                var strPermitId = strDealParam.substring(idxPound + 1);
                
		        Chenxin.ajax({
				    type: "post",
				    dataType: "json",
				    url: _this.actionUrl,
				    data: {op:'Get',PermitId:strPermitId},
				    onSuccess: function(data) {
				        //删除按钮显示
                        $("#btDelEditPermit").show();
					    _this.form.reset();
					    _this.form.currentModel=data;
					    data.hidPermitCode = data.PermitCode;
					    _this.form.fitData(data);
					    _this.form.show();
		                _this.form.setTitle('权限编辑');
				    }
			    });
            }
        });
    },
    refreshPermits:function(){
        //获得权限列表
        Chenxin.ajax({		
			type: "get",
			dataType: "json",
			paramType:'json',
			url: this.actionUrl,
			data:{op:'GetAll'},
			onSuccess:function(jsonRet){
		        //添加到权限模块
                $("#divPermitContent").html(jsonRet);
			}
		});
    },
	initComponent:function(){
		Chenxin.UI.PermitManage.superclass.initComponent.call(this);
	}
});

/**
*权限编辑
*/
Chenxin.UI.EditPermitForm=Chenxin.extend(Chenxin.UI.FormCommon,{	
	actionUrl:'/service/permitMange.ashx',
	id:'jqmEditPermit',
	renderTo:'divEditPermit',
	winCls:'',
	jqmWidth:450,
	createComboList:function(){
	    var selPermitType = new Chenxin.UI.Combo({
		                        containerId:'#PermitGroup',
		                        tbxWidth:55,
		                        required:true,
		                        items:comboCommon.permitType
			                });
        var selShowLevel = new Chenxin.UI.Combo({
            containerId:'#ShowLevel',
            tbxWidth:55,
            colWidth:80,
            required:true,
            items:comboCommon.getShowLevel($("#ctl00_main_hidShowLevel").val())
        });	
		return [selPermitType,selShowLevel];
	},
	initFormValidator:function(){
	    var _this=this;
		//权限编号验证
        $("#PermitCode").formValidator({ validatorgroup:this.id,
            onshow: "请输入权限编号", onfocus: "请输入权限编号", oncorrect: "OK"
        }).inputValidator({
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "权限编号不能为空!" }, onerror: "权限编号不能为空!"
        }).functionValidator({
            fun:function(val) {
                //编辑的场合，原Code不为空
                if( $("#hidPermitCode").val() != "" ){
                    if( val.toLowerCase() == $("#hidPermitCode").val().toLowerCase() ){
                        return true;
                    }
                }
                //返回结果
                var bRet;
                //存在验证
                $.ajax({
                    type:"post",
		            dataType: "json",
		            async:false,
		            url:_this.actionUrl,
		            data:{op:'ExistsCode',PermitCode:val},
		            success:function(jsonRet) {
                        //没有权限
                        if (jsonRet.success == "0") {
                            alert(jsonRet.msg);
                            bRet = false
                        //时间过期
                        }else if (jsonRet.success == "-1") {
                            alert("超时");
                            bRet = false;
                        //有权限
                        }else{
                            var strData = jsonRet.data;
                            //存在权限编号
                            if(strData == true){
                                bRet = false;
                            }else{
                                bRet = true;
                            }
                        }
                    }
                });
                return bRet;
            }, onerror: "该编号已存在"
	    });
    	
        //权限名称验证
        $("#PermitName").formValidator({ validatorgroup:this.id,
            onshow: "请输入权限名称", onfocus: "请输入权限名称", oncorrect: "OK"
        }).inputValidator({
            min: 1, empty: { leftempty: false, rightempty: false, emptyerror: "权限名称不能为空!" }, onerror: "权限名称不能为空!"
        });
	},
	createForm:function(){
		return formCommon.EditPermitForm('EditPermit');
	},
	initEvent:function(){
	    var _this=this;
	    /*************************事件：添加权限************************/
		$('#btSaveEditPermit').live('click',function(){
		    //ID为空，添加
		    if( $("#PermitID").val() == "" ){
		        _this.op = "Add";
		    }else{
		        _this.op = "Edit";
		    }
			_this.save();
		});
		
		/*************************事件：关闭窗口************************/
		$('#btCloseEditPermit').live('click',function(){
			_this.hide();
		});	
		
		/*************************事件：删除权限************************/
		$('#btDelEditPermit').live('click',function(){
		    _this.del($("#PermitID").val());
		});	
	},
	saveSucc:function(){
	    var _this=this;
	    _this.refreshPermits();
	},
	afterDelSucc:function(){
	    var _this=this;
	    _this.refreshPermits();
	},
	initComponent:function(){
		Chenxin.UI.EditPermitForm.superclass.initComponent.call(this);
	}
});

$(document).ready(function(){

	this.permitManage=new Chenxin.UI.PermitManage();	
});